package com.hungry.hfw.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public abstract class HungryAbstractNervousSystem extends AbstractController {

	protected static final Logger logger = Logger.getLogger(HungryAbstractNervousSystem.class);

	private String success;
	private String error;
	private String message;
	
	public String getSuccess() {
		return success;
	}

	public void setSuccess(String success) {
		this.success = success;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = null;
		mv = new ModelAndView();
		try {
			mv = doControlFlow(request, response);
		} catch(Throwable er) {
			er.printStackTrace();
			System.out.println("inside exception");
			logger.error("Caught Exception at abstract controller", er);
			if(error!=null) {
				mv = new ModelAndView(error);
				mv.getModelMap().put("message", message);
				

			} else if(success!=null) {
				mv = new ModelAndView(success);

			}
			else {
				String reply = "Error Processing your request. Please try after some time";
				mv = new ModelAndView();
				mv.getModelMap().put("reply", reply);
			}
		} finally {
			mv.getModelMap().put("message", message);
		}
		
		return mv;
	}
	
	public abstract ModelAndView doControlFlow(HttpServletRequest request,
			HttpServletResponse response)  throws Throwable;

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}


}
